<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use PHPQRCode\QRcode;
// use think\Request;

class Index extends Controller
{   

    public function makeQrcode($url) {
        QRcode::png($url, false, 'L', 4, 2, true);
        exit();
    }

    public function ttest() {
        $merchantOutOrderNo = '201911776282232';
        $merid = '100001';
        $noncestr = '323124jasaf';
        $notifyUrl = 'https://www.test.com/asjc';
        $orderMoney = '5.60';
        // $orderTime = date('YmdHis',time());
        $orderTime = '20170601123456';
        $sign = 'wuhLhyqW4kB4Q4yOrwH80HuVnXNSehOr';


        $url = 'http://jhpay.yizhibank.com/api/createPcOrder';

        $postData = 'merchantOutOrderNo=' . $merchantOutOrderNo
            . '&merid=' . $merid
            . '&noncestr=' . $noncestr
            . '&notifyUrl=' . $notifyUrl
            . '&orderMoney=' . $orderMoney
            . '&orderTime=' . $orderTime 
            ;
        $sign = md5($postData . '&key=wuhLhyqW4kB4Q4yOrwH80HuVnXNSehOr');
        $postData .= '&sign=' .$sign;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $output = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($output, true);
        $this->makeQrcode($res['url']);
        // QRcode::png("http://jhpay.yizhibank.com/api/pcOrder?code=MjAxOTExNzc2MjgyMjMyJjEwMDAwMQ==", false, 'L', 4, 2, true);


        
        // $res = QRcode::png("http://jhpay.yizhibank.com/api/pcOrder?code=MjAxOTExNzc2MjgyMjMyJjEwMDAwMQ==", 'L', 4, 2);
        // dump($res);
    
    }




    private $num = 5;//每个分类显示商品数量

    /**
     * 前台首页
     */
    public function index()
    {	
        $cateList = DB::name('cate')->order('id asc')->select();
        //Sql语句待优化
        $item = DB::name('Goods')
                ->field('a.id, a.gName, a.mPrice')
                ->alias('a')
                ->join('__GOODS__ d', '(a.cId = d.cId AND a.pubTime < d.pubTime)', 'LEFT')
                ->group('a.cId, a.id')
                ->having('count(d.id) < ' . $this->num)
                ->order('a.cId asc, a.pubTime desc')
                ->select(); 
        
        foreach ($item as &$v) {
            $v['albumPath'] = DB::name('album')->field('albumPath')->where('gid', $v['id'])->limit('1')->select()['0']['albumPath'];
        }
        unset($v);
        $itemList=array_chunk($item, $this->num);//存在bug 需要每个分类下至少有$num个商品
        for ($i=0; $i < count($itemList); $i++) { 
            $list[$i] = $cateList[$i];
            $list[$i]['item'] = $itemList[$i]; 
        }
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function test()
    {
        // $cateList = DB::name('cate')->order('id asc')->select();w
        // dump($cateList);
        // $res = DB::name('Goods')
        //         ->field('a.id, a.gName, a.mPrice')
        //         ->alias('a')
        //         // ->join('__ALBUM__ b', 'a.id = b.gid ', 'LEFT')
        //         // ->join('__CATE__ c', 'a.cId = c.id', 'LEFT')
        //         ->join('__GOODS__ d', '(a.cId = d.cId AND a.pubTime < d.pubTime)', 'LEFT')
        //         ->group('a.cId, a.id')
        //         ->having('count(d.id) < 4')
        //         ->order('a.cId asc, a.pubTime desc')
        //         ->select(); 
        
        // foreach ($res as $key => &$value) {
        //     $value['albumPath'] = DB::name('album')->field('albumPath')->where('gid', $value['id'])->limit('1')->select()['0']['albumPath'];
        // }
        // unset($value);
        // dump($res);
        // $itemList=array_chunk($res, '4');
        // dump($itemList);
        // for ($i=0; $i < count($itemList); $i++) { 
        //     $list[$i] = $cateList[$i];
        //     $list[$i]['item'] = $itemList[$i]; 
        // }
        // dump($list);
        // 
        // 
        // $filename = APP_PATH.'index/view/index/nav.json';
        // $file = file_get_contents($filename);
        // $menu = json_decode($file,true);
        // $this->assign('menu', $menu);
        
        // $cateList = DB::name('cate')->order('id asc')->select();
        // $item = DB::name('Goods')
        //         ->field('a.id, a.gName, a.mPrice')
        //         ->alias('a')
        //         // ->join('__ALBUM__ b', 'a.id = b.gid ', 'LEFT')
        //         // ->join('__CATE__ c', 'a.cId = c.id', 'LEFT')
        //         ->join('__GOODS__ d', '(a.cId = d.cId AND a.pubTime < d.pubTime)', 'LEFT')
        //         ->group('a.cId, a.id')
        //         ->having('count(d.id) < 5')
        //         ->order('a.cId asc, a.pubTime desc')
        //         ->select(); 
        
        // foreach ($item as &$v) {
        //     $v['albumPath'] = DB::name('album')->field('albumPath')->where('gid', $v['id'])->limit('1')->select()['0']['albumPath'];
        // }
        // unset($v);
        // $itemList=array_chunk($item, '5');
        // for ($i=0; $i < count($itemList); $i++) { 
        //     $list[$i] = $cateList[$i];
        //     $list[$i]['item'] = $itemList[$i]; 
        // }


        // $this->assign('list', $list);
        // return $this->fetch();
        
    }
}
